package at.htlkaindorf.jsf.application;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.faces.bean.ApplicationScoped;
import javax.faces.bean.ManagedBean;

import at.htlkaindorf.jsf.domain.Entry;

/**
 * @author david.tomes
 */
@ManagedBean(name = "dataBean")
@ApplicationScoped
public class DataBean implements Serializable {

    private static final long serialVersionUID = 4352215871073435967L;

    private List<Entry> guestbookEntries = new ArrayList<Entry>();

    private Long currentId = 0L;

    public DataBean() {
        addTestEnries();
    }

    public void deleteEntry(Entry entry) {
        guestbookEntries.remove(entry);
    }

    public synchronized void deleteEntry(long id) {
        Entry entry = getEntryById(id);
        if (entry != null) {
            guestbookEntries.remove(entry);
        }
    }

    private void addTestEnries() {
        Date now = new Date();
        // addNewEntry("Arno", "arno@infonova.com", "Re: JSF 2.2 Forum", "Wooohoooo :D");
        guestbookEntries.add(new Entry(currentId++, "Arno", "arno@infonova.com", new Date(now.getTime() - 6000),
            "Re: JSF 2.2 Forum", "Wooohoooo :D"));
        guestbookEntries.add(new Entry(currentId++, "David", "david@infonova.com", new Date(now.getTime() - 12000),
            "Re: JSF 2.2 Forum", "Viel Spass! :)"));
        guestbookEntries.add(new Entry(currentId++, "Christian", "christian@infonova.com", new Date(
            now.getTime() - 18000), "JSF 2.2 Forum", "Das ist ein JSF Testforum."));
    }

    public synchronized void addNewEntry(String name, String email, String title, String text) {
        if (name == null || name.isEmpty()) {
            throw new IllegalArgumentException("Name must be not empty");
        }
        if (email == null || email.isEmpty()) {
            throw new IllegalArgumentException("Email must be not empty");
        }
        if (title == null || title.isEmpty()) {
            throw new IllegalArgumentException("Title must be not empty");
        }
        if (text == null || text.isEmpty()) {
            throw new IllegalArgumentException("Text must be not empty");
        }
        Entry entry = new Entry(currentId, name, email, new Date(), title, text);
        guestbookEntries.add(entry);
        currentId++;
    }

    public synchronized void addEntry(Entry entry) {
        entry.setId(currentId);
        currentId++;
        guestbookEntries.add(entry);
    }

    public boolean isEntryExisting(Long id) {
        return getEntryById(id) != null;
    }

    public Entry getEntryById(Long id) {
        for (Entry e : guestbookEntries) {
            if (e.getId() == id) {
                return e;
            }
        }
        return null;
    }

    public List<Entry> getGuestbookEntries() {
        return guestbookEntries;
    }

    public void setGuestbookEntries(List<Entry> guestbookEntries) {
        this.guestbookEntries = guestbookEntries;
    }
}
